package top.deepdesigner.controller.market;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.dto.CommentReplyDTO;
import top.deepdesigner.dto.OrderShipDTO;
import top.deepdesigner.pojo.DtsOrder;
import top.deepdesigner.pojo.DtsOrderGoods;
import top.deepdesigner.pojo.DtsUser;
import top.deepdesigner.service.DtsCommentService;
import top.deepdesigner.service.DtsOrderGoodsService;
import top.deepdesigner.service.DtsOrderService;
import top.deepdesigner.service.DtsUserService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonCatVO;
import top.deepdesigner.vo.CommonListVO;
import top.deepdesigner.vo.UserVo;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 19:53:33 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/order")
public class OrderController {
  @Autowired
  private DtsOrderService orderService;

  @Autowired
  private DtsOrderGoodsService orderGoodsService;

  @Autowired
  private DtsUserService userService;

  @Autowired
  private DtsCommentService commentService;

  @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:order:list"})
  @GetMapping("/list")
  public Object list(
      @RequestParam HashMap<String, Object> params,
      @RequestParam(defaultValue = "") Integer userId,
      @RequestParam(defaultValue = "") String orderSn,
      @RequestParam(defaultValue = "") Integer[] orderStatusArray) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");


    CommonListVO<DtsOrder> orderListVO = new CommonListVO<>();
    //获取分页数量
    Long count = orderService.getOrderCount();
    List<DtsOrder> dtsCategories = orderService.getOrderList(page, limit, sort, order, userId, orderSn, orderStatusArray);
    orderListVO.setTotal(count);
    orderListVO.setItems(dtsCategories);
    return ResponseUtil.ok(orderListVO);
  }

  @GetMapping("/listShipChannel")
  public Object listShipChannel() {
    //获取分页数量
    List<CommonCatVO<String, String>> shipChannels = orderService.getShipChannelList();
    HashMap<String, Object> map = new HashMap<>();
    map.put("shipChannelList", shipChannels);
    return ResponseUtil.ok(map);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "详情")
  @RequiresPermissions(value = {"admin:order:detail"})
  @GetMapping("/detail")
  public Object detail(@RequestParam Integer id) {
    HashMap<String, Object> map = new HashMap<>();

    DtsOrder dtsOrder = orderService.getOrderById(id);

    List<DtsOrderGoods> orderGoods = orderGoodsService.getOrderGoodsByOrderId(id);

    UserVo userVo = new UserVo();
    if (dtsOrder != null) {
      DtsUser dtsUser = userService.getUserById(dtsOrder.getUserId());
      userVo.setNickname(dtsUser.getNickname());
      userVo.setAvatar(dtsUser.getAvatar());
    }

    map.put("orderGoods", orderGoods);
    map.put("user", userVo);
    map.put("order", dtsOrder);
    return ResponseUtil.ok(map);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "回复")
  @RequiresPermissions(value = {"admin:order:orderreply"})
  @PostMapping("/reply")
  public Object reply(@RequestBody CommentReplyDTO commentReplyDTO) {
    Boolean b = commentService.replyComment(commentReplyDTO);
    if (b) return ResponseUtil.ok();
    return ResponseUtil.fail();
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "发货")
  @RequiresPermissions(value = {"admin:order:ship"})
  @PostMapping("/ship")
  public Object ship(@RequestBody OrderShipDTO orderShipDTO) {
    //402 系统收获
    orderShipDTO.setOrderStatus(402);
    Boolean b = orderService.shipGoods(orderShipDTO);
    if (b) return ResponseUtil.ok();
    return ResponseUtil.fail();
  }
}
